package com.wyz.socket.core.base.frame.receive;

import com.wyz.socket.core.base.IoArgs;
import com.wyz.socket.core.base.frame.Frame;

import java.io.IOException;

public abstract class AbstractReceiveFrame extends Frame {

    volatile int bodyRemaining ;

    public AbstractReceiveFrame(byte[] header) {
        super(header);
        bodyRemaining = getBodyLength() ;
    }

    @Override
    public synchronized boolean handle(IoArgs args) throws IOException {
        if(bodyRemaining == 0){
            return false;
        }
        bodyRemaining -= consumeBody(args) ;

        return bodyRemaining == 0 ;
    }

    protected abstract int consumeBody(IoArgs args) throws IOException;

    @Override
    public Frame nextFrame() {
        return null;
    }

    @Override
    public int getUnConsumeLength() {
        return bodyRemaining ;
    }
}
